﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Ivony.Html;
using Ivony.Html.Parser;

namespace ZX.EHR.ResumeParseLib
{
    /// <summary>
    /// 智联招聘
    /// </summary>
    public class ZhiLianZhaoPinParsing : IHtmlParsable
    {
        public ResumeParsedInfo Parsing(string htmlContent)
        {
            if (string.IsNullOrEmpty(htmlContent))
                throw new ArgumentNullException("htmlContent");

            ResumeParsedInfo resumeInfo = null;
            var doc = new JumonyParser().Parse(htmlContent);
            if (doc != null)
            {
                if (doc.Find("link").Count() > 0)
                {
                    if (true == doc.Find("link").ElementAt(0).Attribute("href").AttributeValue.Contains("zhaopin"))
                    {
                        resumeInfo = new ResumeParsedInfo();
                        try
                        {
                            resumeInfo.Name = doc.GetElementById("resumeDetail").Descendants().ElementAt(0).Descendants().ElementAt(0).Descendants().ElementAt(0).Descendants().ElementAt(0).InnerHtml();
                            
                        }
                        catch { }
                        try
                        {
                            //电话号码
                            //string[] tempToken=doc.FindSingle(".baseinfo").InnerText().Split(new char[] { '|' });
                            //string tel=string.Empty;
                            //string tempStr = string.Empty;
                            //foreach(string s in tempToken)
                            //{
                            //    if(s.Contains("手机"))
                            //    {
                            //        tempStr = s;
                            //        break;
                            //    }
                            //}
                            //if (tempStr != string.Empty)
                            //{
                            //    string[] tempToken2 = tempStr.Split("\r\n".ToCharArray());
                            //    foreach (string str in tempToken2)
                            //    {
                            //        if (str.Contains("手机"))
                            //        {
                            //            tel = str.Substring(0, 11);
                            //            break;
                            //        }
                            //    }
                            //}
                            resumeInfo.PhoneNO = doc.GetElementById("resumeDetail").Descendants().ElementAt(0).Descendants().ElementAt(0).Descendants().ElementAt(0).Descendants().ElementAt(1).InnerHtml().Split('：')[1];
                        }
                        catch { }
                        try
                        {
                            //email
                            //resumeInfo.Email = doc.FindSingle(".baseinfo").InnerText().Split(new char[] { '|' })[5].Split(new char[] { '\n' })[3].Split(new char[] { ':' })[1].Trim();
                            string[] ton= doc.Find(".summary-bottom").ElementAt(0).InnerText().Split("\r\n".ToCharArray());
                            resumeInfo.Email = ton[ton.Length - 1].Split('：')[1];
                        }
                        catch { }
                        string tempStr = doc.Find(".summary-top").ElementAt(0).InnerText().Split("\r\n".ToCharArray())[0].Trim();
                        try
                        {
                            tempStr = tempStr.Replace("    ", " ");
                            string[] tempToken = tempStr.Split(' ');
                            int n=0;
                            foreach(string s in tempToken)
                            {
                                if (s.Trim().Contains("大专") || s.Trim().Contains("本科") || s.Trim().Contains("硕士") || s.Trim().Contains("博士") || s.Trim().Contains("博士后"))
                                {
                                    resumeInfo.Degree = s;//学历
                                }
                                if (s.Trim().Contains("男") || s.Trim().Contains("女"))
                                {
                                    resumeInfo.Degree = s;//学历
                                }
                                if (s.Trim().Contains("岁"))
                                {
                                    string temp = s;
                                    int index1 = temp.IndexOf("(");
                                    int index2 = temp.IndexOf(")");
                                    string[] tempTokenY = temp.Substring(index1 + 1, index2 - index1 - 1).Split("年".ToCharArray());
                                    string birthDayY = tempTokenY[0].Trim();
                                    string[] tempTokenM = tempTokenY[1].Trim().Split("月".ToCharArray());
                                    string birthDayM = tempTokenM[0].Trim();
                                    resumeInfo.BirthDay = birthDayY + "-" + birthDayM + "-01";//出生日期
                                }
                                if (s.Trim().Contains("年工作经验"))
                                {
                                    resumeInfo.WorkYears = s.Split("年工作经验".ToCharArray())[0];
                                }
                            }
                            
                        }
                        catch { }
                        try
                        {
                            string temp = doc.FindSingle(".summary-bottom").InnerText();
                            string[] token = temp.Split("\r\n".ToCharArray());
                            foreach (string s in token)
                            {
                                if (s.Contains("身份证"))
                                {
                                    resumeInfo.IDCardNO = s.Split('：')[1];
                                }
                            }
                        }
                        catch { }
                        try
                        {
                            //申请岗位
                            //resumeInfo.ApplyPosition = doc.Find("div").First().Find("table").First().Find("td").First().InnerText().Split(new char[] { '：' })[1];
                        }
                        catch { }
                        resumeInfo.FullHtmlContent = htmlContent;
                    }
                    else
                    {
                        throw new ApplicationException("请检查您上传的是否智联招聘的简历");
                    }
                }
                else
                {
                    throw new ApplicationException("无法识别的简历类型");
                }
            }
            return resumeInfo;
        }
    }
}
